package cn.edu.zut.cs.Business;

import cn.edu.zut.cs.DAO.StudentDAO;
import cn.edu.zut.cs.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GradeService {
    StudentDAO studentDAO;

    @Autowired
    public void setStudentDAO(StudentDAO studentDAO){
        this.studentDAO=studentDAO;
    }
    public void insert(Student student){
        studentDAO.insertStudent(student);
    }
    @Transactional
    public float GradeValue(){
        List<Student> bookList =studentDAO.queryAll();
        float score=0;
        int num=0;
        for(Student b:bookList){
            score+=b.getScore();
            num++;
            if(b.getScore()<60)
                b.setGrade('E');
            else if(b.getScore()<70)
                b.setGrade('D');
            else if(b.getScore()<80)
                b.setGrade('C');
            else if(b.getScore()<90)
                b.setGrade('B');
            else
                b.setGrade('A');
            studentDAO.modifyStudent(b);
        }
        score/=(float)num;
        return score;
    }

}
